﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using org.se.fs.presenter.Library;

namespace org.se.fs.presenter.View.Template
{
    public abstract class ViewLists
    {
        public event FormEvent.FormEventHandler OnToggle;
        public event FormEvent.FormEventHandler OnDelete;
        public event FormEvent.FormEventHandler OnError;
        protected DataTable Lists { get; set; }
        protected UInt16 CurrPage { get; set; }
        protected UInt16 MaxRecords { get; set; }
        protected IList<SearchOption> SearchOption;

        public virtual void Initialize() { }

        public void TriggerToggle(FormEvent e)
        {
            if (OnToggle != null)
                OnToggle(this, e);
        }

        public void TriggerDelete(FormEvent e)
        {
            if (OnDelete != null)
                OnDelete(this, e);
        }

        public void TriggerError(FormEvent e)
        {
            if (OnError != null)
                OnError(this, e);
        }

        protected virtual void TaskOnFetchLists(object source, FormEvent e) { }

        protected virtual void TaskOnFetch(object source, FormEvent e) { }

        protected virtual void TaskOnDelete(object source, FormEvent e) { }

        protected virtual void TaskOnToggle(object source, FormEvent e) { }

        protected virtual void TaskOnGetCount(object source, FormEvent e) { }

        protected virtual void InitializeSearchOption() { }

        public virtual void Add(DataTable data)
        {
            if (data == null || data.Rows.Count.Equals(0))
                return;

            foreach (DataRow dr in data.Rows)
                Lists.ImportRow(dr);
            DisplayData();
        }

        public virtual void Modify(DataTable data)
        {
            if (data == null || data.Rows.Count.Equals(0))
                return;

            foreach (var dr in Lists.Select())
            {
                if (!dr["id"].ToString().Equals(data.Rows[0]["id"].ToString()))
                    continue;
                foreach (DataColumn dc in Lists.Columns)
                {
                    var dc1 = dc;
                    foreach (var ddc in data.Columns.Cast<DataColumn>().Where(ddc => dc1.ColumnName.Equals(ddc.ColumnName)))
                    {
                        dr[dc] = data.Rows[0][ddc];
                        break;
                    }
                }
            }
            DisplayData();
        }

        public virtual void Remove(DataTable data) { }

        public virtual void Toggle(uint index, bool active) { }

        public virtual void Search() { }

        protected virtual string GetFilterString()
        {
            return string.Empty;
        }

        public virtual DataTable GetData(int index)
        {
            var a = new DataTable();
            foreach (DataColumn dc in Lists.Columns)
                a.Columns.Add(dc.ColumnName, dc.DataType);

            if (index > Lists.Rows.Count - 1)
                return a;

            a.ImportRow(Lists.Rows[index]);
            return a;
        }

        public virtual void DisplayData() {}

        protected virtual void SetStatusPage() { }

        public virtual DataTable EmptyRecord()
        {
            using (var a = new DataTable())
            {
                foreach (DataColumn dc in Lists.Columns)
                    a.Columns.Add(dc.ColumnName, dc.DataType);
                return a;
            }
        }

        protected virtual void FetchRecords() { }

        public virtual void MoveToFirstRecord()
        {
            CurrPage = 0;
            FetchRecords();
        }

        public virtual void MoveToPreviousRecord()
        {
            if (CurrPage < Config.MaxPageSize)
            {
                MoveToFirstRecord();
                return;
            }

            CurrPage -= Config.MaxPageSize;
            FetchRecords();
        }

        public virtual void MoveToNextRecord()
        {
            if (CurrPage < Config.MaxPageSize)
            {
                MoveToFirstRecord();
                return;
            }

            CurrPage += Config.MaxPageSize;
            FetchRecords();
        }

        public virtual void MoveToLastRecord()
        {
            CurrPage += Config.MaxPageSize;
            FetchRecords();
        }
    }
}